package com.hellogood.yaohuo.client.common;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.entity.Org;
import com.hellogood.yaohuo.shared.services.OrgService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.KeyPressEvent;
import com.smartgwt.client.widgets.events.KeyPressHandler;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class OrgTreeWindow extends Window {
	private TreeNode[] treeNodes = null;

	private TextItem textItem = null;
	private TextItem selectItem = null;
	private TreeGrid treeGrid = null;

	public OrgTreeWindow(TextItem textItem, TextItem selectItem) {
		this.textItem = textItem;
		this.selectItem = selectItem;
		initData();
		initView();
		setIsModal(true);	
		addCloseClickHandler(new CloseClickHandler() {

			@Override
			public void onCloseClick(CloseClickEvent event) {
				destroy();
			}
		});
	}

	private void initData() {
		String staffCode = LoginUser.getInstance().getCurrentUser().getStaffCode();
		OrgService.Util.getInstance().getOrgListByStaffCode(staffCode, new OrgAsynCallback());
	}

	private void initView() {
		TreeGridField nodeIdField = new TreeGridField("nodeId");
		nodeIdField.setHidden(true);
		TreeGridField nameField = new TreeGridField("name", "组织");
		treeGrid = new TreeGrid();
		treeGrid.setFields(nodeIdField, nameField);
		treeGrid.setWidth("100%");
		treeGrid.setHeight("95%");
		treeGrid.setCanFocus(true);

		treeGrid.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				System.out.println(event.getKeyName());
				if (event.getKeyName().equals("Enter")) {
					System.out.println(event.getKeyName());
				}

			}
		});

		treeGrid.setGenerateDoubleClickOnEnter(true);
		treeGrid.addRecordDoubleClickHandler(new RecordDoubleClickHandler() {

			@Override
			public void onRecordDoubleClick(RecordDoubleClickEvent event) {
				System.out.println("double click");
				Record record = event.getRecord();
				if (record != null) {
					textItem.setValue(record.getAttribute("name"));
					selectItem.focusInItem();
					OrgTreeWindow.this.setVisible(false);
					OrgTreeWindow.this.destroy();
				}
			}
		});

		treeGrid.setTop(20);
		addChild(treeGrid);
	}

	public TreeGrid getTreeGrid() {
		return treeGrid;
	}

	private class OrgAsynCallback implements AsyncCallback<List<Org>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Org> result) {
			if (result != null) {
				treeNodes = new TreeNode[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Org vo = result.get(i);
					TreeNode r = new TreeNode();
					r.setAttribute("nodeId", vo.getOrgCode());
					r.setAttribute("code", vo.getOrgCode());
					r.setAttribute("name", vo.getOrgCode() + "-" + vo.getOrgName());
					r.setAttribute("isOpen", true);
					r.setAttribute("parentId", "0");
					treeNodes[i] = r;
				}

				Tree tree = new Tree();
				tree.setModelType(TreeModelType.PARENT);
				tree.setNameProperty("name");
				tree.setRootValue("0");
				tree.setIdField("nodeId");
				tree.setParentIdField("parentId");
				tree.setOpenProperty("isOpen");
				tree.setData(treeNodes);

				treeGrid.setData(tree);
				if (treeNodes.length > 0) {
					treeGrid.selectRecord(0);
				}
			}
		}
	}
}
